package com.gitee.hifong45.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.gitee.hifong45.FE;
import com.gitee.hifong45.Msg;
import com.gitee.hifong45.Ret;
import com.gitee.hifong45.Str;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: fred_deng
 */
@Configuration
@Slf4j
@RestControllerAdvice
public class ExtExceptionHandler {
    /**
     * 快速异常
     * @param e 异常
     * @return 响应
     */
    @ExceptionHandler(FE.class)
    public Ret<Object> handleFEException(FE e) {
        Ret<Object> ret = Ret.Fail(e.code, e.msg + Str.orElse(e.extMsg, ""));
        log.error(ret.toJsonStr());
        return ret;
    }

    @ExceptionHandler(value=Exception.class)
    public Ret<Object> handleAllExcepiton(Exception e, HttpServletRequest request) {
        log.error("{} exception: ", request.getRequestURI(), e);
        return Ret.Fail(Msg.ACCESS_FAILED);
    }
}
